﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using EquipSecs300;
using SecsEquip;

namespace EquipSecs300
{
    public partial class TitleView : CMyFormEx
    {
        // 委托
        delegate void SetCommState(COMM_STATE nState);
        delegate void SetControlState(CONTROL_STATE nState);
        SetCommState m_pComm;
        SetControlState m_pControl;
        Action<object, ClockEventArgs> OnClockEventHandlerView;
        FormPassword frm;

        public TitleView(FormPassword formPassword)
        {
            InitializeComponent();
            frm = formPassword;

            m_pComm = CommunicationStateChange;
            m_pControl = ControlStateChange;
            OnClockEventHandlerView = OnControlClockEventHandler;

        }

        // 更新数据
        public override void UpdateData()
        {
            // 状态改变事件
            m_pSecs.m_pOnCommState += OnCommState;
            m_pSecs.m_pOnControlState += OnControlState;
            m_pSecs.m_pOnClock += OnClockEventHandler;

        }

        // 响应状态改变事件
        // Invoke 转接到创建线程
        // 使用线程，避免Invoke死锁
        void OnCommState(object sender, CommStateEventArgs e)
        {
            BeginInvoke(m_pComm, e.nState);
        }

        // 响应状态改变事件
        // Invoke 转接到创建线程
        // 使用线程，避免Invoke死锁
        void OnControlState(object sender, ControlStateEventArgs e)
        {
            BeginInvoke(m_pControl, e.nState);
        }

        // 改变状态显示
        void CommunicationStateChange(COMM_STATE nState)
        {
            labelCommState.Text = CSecsAssist.GetCommunicationText(nState);
        }

        // 改变状态显示
        void ControlStateChange(CONTROL_STATE nState)
        {
            labelControlState.Text = CSecsAssist.GetControlText(nState);
        }

        private void OnClockEventHandler(object sender, ClockEventArgs e)
        {
            BeginInvoke(OnClockEventHandlerView, sender, e);
        }

        private void OnControlClockEventHandler(object sender, ClockEventArgs e)
        {
            MessageBox.Show($"收到EAP时间：{e.time.ToString()}");
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {

            if (GlobalVar.IsLogIn <= 0)
            {
                frm.ShowForm();
            }
            else
            {
                frm.PassWordLogout();
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (GlobalVar.IsLogIn <= 0)
            {
                //btnLogin.Text = "Login";
            }
            else
            {
                //btnLogin.Text = "Logout";
            }

            labelTime.Text = DateTime.Now.ToString();

        }

        private void labelTime_Click(object sender, EventArgs e)
        {
            //SysTimeHelper sysTime = new SysTimeHelper();
            //DateTime dateTime = new DateTime(2020, 12, 31, 10, 21, 58);
            //sysTime.SetSysTime(dateTime);
        }
    }
}
